package com.uniledger.exchange.bank;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.uniledger.exchange.R;
import com.uniledger.exchange.glide.GlideApp;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class BottomBankAdapter extends BaseAdapter {

    List<BankCard> cardList;
    LayoutInflater inflater;
    Context context;

    public static final int FLAG_CARD = 0;
    public static final int FLAG_ADD = 1;
    public static final int FLAG_COUNT = 2;


    public BottomBankAdapter(Context context) {
        this.context = context;
        this.inflater = LayoutInflater.from(context);
        this.cardList = BankCard.getBankList();
        this.cardList.add(new BankCard());
    }

    public BottomBankAdapter(Context context, List<BankCard> list) {
        this.context = context;
        this.inflater = LayoutInflater.from(context);
        this.cardList = list;
        this.cardList.add(new BankCard());
    }


    @Override
    public int getCount() {
        return cardList.size();
    }

    @Override
    public BankCard getItem(int position) {
        return cardList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public int getItemViewType(int position) {
        if (getCount() - 1 == position) {//最后一个条目
            return FLAG_ADD;
        } else {
            return FLAG_CARD;
        }
    }

    @Override
    public int getViewTypeCount() {
        return FLAG_COUNT;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        AddViewHolder addHolder = null;
        int type = getItemViewType(position);
        if (convertView == null) {
            if (type == FLAG_CARD) {
                convertView = inflater.inflate(R.layout.item_bank_bottom, parent, false);
                holder = new ViewHolder(convertView);
                convertView.setTag(holder);
            } else {
                convertView = inflater.inflate(R.layout.item_add_bank_bottom, parent, false);
                addHolder = new AddViewHolder(convertView);
                convertView.setTag(addHolder);
            }
        } else {
            if (type == FLAG_CARD) {
                holder = (ViewHolder) convertView.getTag();
            } else {
                addHolder = (AddViewHolder) convertView.getTag();
            }
        }

        BankCard card = getItem(position);

        if (type == FLAG_CARD) {
            holder.name.setText(card.bank);
            GlideApp.with(context).load(card.bankIcon)
                    .placeholder(R.mipmap.ic_launcher)
                    .error(R.mipmap.ic_launcher)
                    .circleCrop()
                    .into(holder.icon);
        } else {

        }
        return convertView;
    }

    static class ViewHolder {
        @BindView(R.id.img_bottom_icon)
        ImageView icon;

        @BindView(R.id.txt_bottom_name)
        TextView name;

        @BindView(R.id.txt_bank_limit_desc)
        TextView desc;

        public ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }

    static class AddViewHolder {

        public AddViewHolder(View view) {

        }
    }
}
